package com.example.demo.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class MyNetworkUtil {
    //token令牌
    public static String sid = "cdb68cc7913c47bc95874b0ae34dc986";

    //请求获取响应数据
    public static String getParam(String method, String url, HashMap<String,Object> data){
        StringBuffer stringBuffer = new StringBuffer();
        boolean flag = "POST".equals(method.trim().toUpperCase());
        InputStream in = null;
        InputStreamReader is = null;
        BufferedReader br = null;
        try {
            if(!flag){
                url = url+"?"+splicing(data);
            }
            URL url1 = new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
            urlConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
            urlConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36");
            if(flag){
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);
                urlConnection.setRequestMethod("POST");
                OutputStream out = urlConnection.getOutputStream();
                OutputStreamWriter owt = new OutputStreamWriter(out);
                BufferedWriter bw = new BufferedWriter(owt);
                ObjectMapper objectMapper = new ObjectMapper();
                String s = objectMapper.writeValueAsString(data);
                bw.write(s);
                bw.close();
                owt.close();
            }
            in = urlConnection.getInputStream();
            is = new InputStreamReader(in);
            br = new BufferedReader(is);
            String line;
            while((line = br.readLine()) != null) {
                stringBuffer.append(line);
            }
            br.close();
            is.close();
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString()==""?null:stringBuffer.toString();
    }

    //字符串拼接
    private static String splicing(HashMap<String, Object> data){
        StringBuffer stringBuffer = new StringBuffer();
        if(data!=null){
            Set<String> strings = data.keySet();
            Iterator<String> iterator = strings.iterator();
            String key;
            while (iterator.hasNext()){
                key = iterator.next();
                stringBuffer.append(key+"="+data.get(key)+"&");
            }
        }
        return stringBuffer.length()>0?stringBuffer.substring(0,stringBuffer.length()-1):stringBuffer.toString();
    }

}
